/*
 * mainForC.c
 *
 *  Created on: 2010-3-11
 *      Author: abc
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <netdb.h>
#define MAXLEN 1024

extern void * multiThreadForC(void* p);
struct message
{
	char flag[15];
	char name[10];
	int size;
	char msg[MAXLEN];
};

struct msq
{
	long msg_type;
	char msg_text[5];
};
int qid = -1,fd = -1,sockfd,savefilefd=-1;

extern void * mutilThreadForC(void* p);
void cutStr(char str[],char left[],int n,char right[],int m,char c)
{
	int i,k,j;
	for(i = 0 ; i < n ;i++)
	{
		if(str[i] == c)
			break;
	}
	if(i == n)
	{
		i = -1;
	}
	else
	{
		memset(left,0,strlen(left));
		for(k = 0 ; k < i ; k++)
		{
			left[k] = str[k];
		}
	}
	for(j = i+1 ; j < m;j++)
	{
		if(str[j] == '\0')
			break;
		right[j-i-1] = str[j];
	}
	left[i] = '\0';
	if(j < m)
		right[j-i-1] = '\0';
	else
		right[m] = '\0';

}

void handlerecvmsg(int *sockfd)
{
	int connfd = *sockfd;
	int nread;
	char buf[1024];
	char str[1024];
	struct message recvmsg;
	time_t timep;
	struct msq msg;
	msg.msg_type = getpid();
	strcpy(msg.msg_text,"OK");
	while(1)
	{

		nread = recv(connfd,&recvmsg,sizeof(struct message),0);

		if(nread == 0)
		{
			printf("与服务器断开了连接\n");
			close(fd);
			close(connfd);
			exit(0);
		}
		else if (strcmp(recvmsg.flag,"all") == 0)
		{
			time (&timep);
			sprintf(str,"%s%s发给所有人:%s\n\n",ctime(&timep),recvmsg.name,recvmsg.msg);
			printf(str,"%s%s发给所有人:%s\n\n",ctime(&timep),recvmsg.name,recvmsg.msg);

		}
		else if (strcmp(recvmsg.flag,"sermsg") == 0)
		{
			time (&timep);
			printf("%s服务器发给所有人:%s\n\n",ctime(&timep),recvmsg.msg);
			continue;
		}
		else if (strcmp(recvmsg.flag,"view") == 0)
		{
			time (&timep);
			printf("%s当前在线客户端:\n%s\n\n",ctime(&timep),recvmsg.msg);
			continue;
		}
	}
}
int main(int argc,char *argv[])
{
	struct sockaddr_in server_addr;
	int port;
	struct message a;
	char str[MAXLEN];
	char buf[MAXLEN];
	pthread_t pid;
	char ipaddr[MAXLEN];
	printf("----------------------------------\n");
	printf("|                                |\n");
	printf("|   input a port number to work       |\n");
	//scanf("%d",&port);
	port=12345;
	printf("|   input a ip address to work       |\n");
	//scanf("%s",ipaddr);
	strcpy(ipaddr,"127.0.0.1");
	if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
	        {
	                printf("创建socket失败\n");
	                exit(1);
	        }

	bzero(&server_addr,sizeof(struct sockaddr_in));
	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = inet_addr(ipaddr);
	server_addr.sin_port = htons(port);
	printf("尝试连接%s,port %d\n",ipaddr,port);
	if(connect(sockfd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr)) == -1)
	{
		printf("与服务器无响应,请隔一段时间再连接\n");
		exit(2);
	}
	pthread_create(&pid,NULL,(void *)handlerecvmsg,(void *)&sockfd);
    pthread_create(&pid,NULL,(void *)multiThreadForC,(void *)&sockfd);
    struct message filedata;
    strcpy(filedata.flag,"login");
            struct hostent* h = gethostent();
            strcpy((char*)filedata.name,(char*)h->h_name);
            if(send(sockfd,&filedata,sizeof(struct message),0)>0){
              printf("send name successfully!!!\n");
            }
    while(1){
    	scanf("%s",str);
    	printf("you input %s\n",str);
    	strcpy(filedata.flag,"all");
    	strcpy((char*)filedata.name,(char*)h->h_name);
    	strcpy(filedata.msg,str);
    	if(send(sockfd,&filedata,sizeof(struct message),0)>0){
    	  printf("send successfully!!!\n");
    	}
    }
        close(sockfd);
        return 0;
}
